/*
 * @lc app=leetcode.cn id=637 lang=javascript
 *
 * [637] 二叉树的层平均值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var averageOfLevels = function (root) {
    let queue = [root]
    let res = []
    while (queue.length) {
        let len = queue.length
        let sum = 0
        let count=len
        while (len) {
            let node = queue.shift()
            sum += node.val
            
            if (node.left) queue.push(node.left)
            if (node.right) queue.push(node.right)
            len--
        }
        res.push(sum / count)
    }
    return res
};
// @lc code=end

